package com.hisense.ovcloud.commons.configs.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.support.destination.DynamicDestinationResolver;

@Configuration
@EnableJms
@Order(1)
@ConditionalOnProperty(prefix = "amq", name = "brokerUrl")
public class AMQConfig {
    private final AMQProps amqConf;

    public AMQConfig(AMQProps amqConf) {
        this.amqConf = amqConf;
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic() {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(new ActiveMQConnectionFactory(amqConf.getUsername(), amqConf.getPassword(), amqConf.getBrokerUrl()));
        bean.setDestinationResolver(new DynamicDestinationResolver());
        bean.setConcurrency(amqConf.getConcurrency());
        return bean;
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue() {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(new ActiveMQConnectionFactory(amqConf.getUsername(), amqConf.getPassword(),amqConf.getBrokerUrl()));
        bean.setDestinationResolver(new DynamicDestinationResolver());
        bean.setConcurrency(amqConf.getConcurrency());
        return bean;
    }
}
